﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.CodeDom;
using System.Xml;
using System.Data;
using System.Net;
using System.IO;
using System.Drawing;
using SmartyOcr;
namespace Lab
{
    class Program
    {
        static void Main(string[] args)
        {
            //Download();
            //OcrTools();
            //asp();
            //basicOcr();
            //draw();

            
            Console.WriteLine(1 << 10);
            //serializer();
            Console.WriteLine("it's over~");
            Console.ReadLine();

            
        }
        private static void Download()
        {
            var html = string.Empty;
            HttpWebRequest request = HttpWebRequest.Create(@"http://guide.fengjing.com/603630/33361_1.shtml") as HttpWebRequest;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse ;
            var stream = response.GetResponseStream();
            var encode = string.IsNullOrEmpty(response.ContentEncoding) ? System.Text.Encoding.GetEncoding(response.CharacterSet) : System.Text.Encoding.GetEncoding(response.ContentEncoding);
            encode = System.Text.Encoding.UTF8;
            var reader = new System.IO.StreamReader(stream, encode);
            html = reader.ReadToEnd();
            System.Html.HtmlDocument doc = new System.Html.HtmlDocument();
            doc.LoadHTML(html);
            var names = doc.SelectSingleNode("//div[@id='wrap']/div[@class='con']/div/div/div/h1/text()");
            var images = doc.SelectSingleNode("//div[@id='STNewsContent']");

            Console.WriteLine(names.Value);

            Console.WriteLine("press any key to continue...");
            Console.ReadLine();
            var h = images.InnerXml;
            Console.WriteLine(images.InnerXml);

            Console.WriteLine("it's over~");

            Console.ReadLine();
        }
        private static void OcrTools()
        {
            //var img = "http://jprice.360buyimg.com/price/gp11048804-1-1-1.png";
            //var request = System.Net.HttpWebRequest.Create(img);
            //var data = request.GetResponse().GetResponseStream();
            //var ms = new System.IO.MemoryStream();
            //var path = Environment.CurrentDirectory+"/" + DateTime.UtcNow.Ticks + ".img";
            //FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            //int b;
            //while ((b=data.ReadByte()) > -1)
            //{
            //    ms.WriteByte((byte)b);
            //    fs.WriteByte((byte)b);
            //}
            //fs.Close();


            //OCRTools.OCR ocr = new OCRTools.OCR();
 
            //ocr.AnalyzeSpecialCharacters = true;
            //ocr.DefaultFolder=@"F:\workspace\lab.xueliang.org\NetBee\Lab\bin\Debug\ocr-tools-sources";
            //ocr.BitmapImage = new System.Drawing.Bitmap(ms);
            //ocr.Process();
            //Console.WriteLine(ocr.Text);
        }
        private static void asp()
        {
            var img = "http://jprice.360buyimg.com/price/gp11048804-1-1-1.png";
            var request = System.Net.HttpWebRequest.Create(img);
            var data = request.GetResponse().GetResponseStream();
            var ms = new System.IO.MemoryStream();
            var path = Environment.CurrentDirectory + "/" + DateTime.UtcNow.Ticks + ".png";
            FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            int b;
            while ((b = data.ReadByte()) > -1)
            {
                ms.WriteByte((byte)b);
                fs.WriteByte((byte)b);
            }
            fs.Close();


            Aspose.OCR.OcrEngine ocr = new Aspose.OCR.OcrEngine();
            ocr.Resource = new FileStream(@"F:\workspace\lab.xueliang.org\NetBee\Lab\Libs\Aspose.OCR.Resouces.zip", FileMode.Open, FileAccess.Read);
            ocr.Languages.AddLanguage(Aspose.OCR.Language.Load(("english")));
            // ocr.Config.AdjustRotation  //旋转校对
            ocr.Config.NeedRotationCorrection = false;
            //ocr.AddRecognitionBlock(Aspose.OCR.RecognitionBlock.FromRectanglePicture(18,0,40,10));
            ocr.Config.OcrAlgorithmMode = Aspose.OCR.OcrAlgorithmMode.None;
            ocr.Image = Aspose.OCR.ImageStream.FromFile(path);
            ocr.DetectTextOnly = true;

            ocr.Process();
            Console.WriteLine(ocr.Text);
        }
        private static void basicOcr()
        {
            #region//
            System.Net.WebClient wb = new System.Net.WebClient();

            byte[] b = wb.DownloadData(@"http://p.blog.csdn.net/images/p_blog_csdn_net/jinjazz/355056/o_rrr.bmp");
            //byte[] b = wb.DownloadData(@"http://jprice.360buyimg.com/price/gp11048804-1-1-1.png");
            System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
            string str = BasicOcr.getNumber(bmp);
            Console.WriteLine(str);  
            #endregion
  
        }

        private static void draw() {
            //ImageCreator.Draw("Liang");
            ImageCreator.Study();
        }

        private static void serializer() {
            var lst = new List<List<int>>(2);
            var l = new List<int>();
            l.Add(1);
            l.Add(2);
            l.Add(3);
            lst.Add(l);
            l.Clear();
            l.Add(4);
            l.Add(5);
            l.Add(6);
            lst.Add(l);
            System.Runtime.Serialization.Json.DataContractJsonSerializer seria = new System.Runtime.Serialization.Json.DataContractJsonSerializer(lst.GetType());
            var ms = new System.IO.MemoryStream();
            seria.WriteObject(ms, lst);
            ms.Position = 0;
            var reader = new StreamReader(ms);
            var json = reader.ReadToEnd();
            Console.Write(json);
        }


        private static void OutputMatrix() {
            var lst = new List<List<int>>();
            lst.Add(new List<int> { 1, 2, 3 });
            lst.Add(new List<int> { 4, 5, 6 });
            lst.Add(new List<int> { 7, 8, 9 });
            foreach (var line in lst)
            {
                foreach (var i in line)
                {
                    Console.Write(i.ToString());
                }
                Console.WriteLine();
            }
        }
    }
}
